﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using HandyControl.Tools.Extension;
using VipReminder.ViewModel;

namespace VipReminder.Views
{
    /// <summary>
    /// CountDown.xaml 的交互逻辑
    /// </summary>
    public partial class CountDown
    {

        // 定时器
        public DispatcherTimer _timer;


        public VmReminder _vm;
        public CountDown()
        {
            InitializeComponent();

            this.AllowsTransparency = true; //窗体支持透明度 
            this.ShowInTaskbar = false;   //任务栏不显示图标
            this.Topmost = false;
            NotifyIcon.Visibility=Visibility.Visible;
        }



        private void Settings_OnClick(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
            NotifyIcon.Visibility = Visibility.Hidden;
            MainWindow mainWindow = new MainWindow();
            Application.Current.MainWindow = mainWindow;
            mainWindow.Show(); 
            this.Close();
        }

        private void Exit_OnClick(object sender, RoutedEventArgs e)
        {
            var confirm = Controls.MessageBox.CreateMessageBox("确认", "确定要退出应用吗?", MessageBoxButton.YesNo);
            if (confirm == MessageBoxResult.Yes)
            {
                this.Close();
                System.Windows.Application.Current.Shutdown();
            }
        } 

        private void NotifyIconContextContent_OnClick(object sender, RoutedEventArgs e)
        {
            //激活窗体并赋予焦点
            this.Activate();
            Application.Current.MainWindow.Activate();
        }


        private void CountDown_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                switch (e.ClickCount)
                {
                    // Background="White"  不加这个属性，不能拖拽
                    case 1://单击
                        {
                            //if (e.LeftButton == MouseButtonState.Pressed)
                            //{
                            //    if (WindowState == WindowState.Maximized)
                            //    {
                            //        WindowState = WindowState.Normal;
                            //    }
                            //}
                            this.DragMove();
                            break;
                        }
                    case 2://双击
                        {
                            break;
                        }
                }
            }
            catch (Exception exception)
            {
            }
        }


        public void InitData()
        {

            _vm = DataContext as VmReminder;

            this.Loaded += CountDown_Loaded;

            if (_vm.IsHourly)
            {

                //下个整点时间段
                var nextHourly = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour + 1, 0, 0);
                var subVal = nextHourly.Subtract(DateTime.Now).Minutes;
                //差值 小于20分钟，算后一个整点
                if (subVal <= 20)
                {
                    _vm.CountDownWorkMinute = _vm.WorkValue + subVal;
                    _vm.CountDownWorkSecond = 0;
                }
                else
                {
                    _vm.CountDownWorkMinute = _vm.WorkValue - (60 - subVal);
                    _vm.CountDownWorkSecond = 0;
                }

                if (_vm.CountDownWorkMinute <= 0)
                {
                    _vm.CountDownWorkMinute = 50;
                }
            }
            else
            {
                _vm.CountDownWorkMinute = _vm.WorkValue;
                _vm.CountDownWorkSecond = 0;
            }

            _timer = _timer ?? new DispatcherTimer();
            _timer.Tick -= Timer_OnTick;
            _timer.Tick += Timer_OnTick;
            _timer.Interval = TimeSpan.FromSeconds(1);
            _timer.Start();
        }

        /// <summary>
        /// 窗口显示在右下角
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CountDown_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
            this.Left = desktopWorkingArea.Right - this.Width;
            this.Top = desktopWorkingArea.Bottom - this.Height;
        }

        private void Timer_OnTick(object sender, EventArgs e)
        {
            Remind();

            //倒计时时结束，弹出休息窗口
            if (_vm.CountDownWorkMinute == 0 && _vm.CountDownWorkSecond == 0)
            {
                _timer.Stop();
                NotifyIcon.Visibility = Visibility.Hidden;
                WorkBreak workBreak = new WorkBreak();
                Application.Current.MainWindow = workBreak;
                workBreak.DataContext = this._vm;
                workBreak.InitData();
                workBreak.ShowDialog();
                this.Close(); 
                return;
            }
            //如果分钟大于0，秒=0，那分-1，秒>0 减秒
            if (_vm.CountDownWorkMinute > 0)
            {
                if (_vm.CountDownWorkSecond > 0)
                {
                    _vm.CountDownWorkSecond--;
                }
                else
                {
                    _vm.CountDownWorkMinute--;
                    _vm.CountDownWorkSecond = 60;
                }
            }

            if (_vm.CountDownWorkMinute == 0)
            {
                _vm.CountDownWorkSecond--;
            }
        }


        private void Remind()
        {
            if (_vm.CountDownWorkMinute == 0 && _vm.CountDownWorkSecond <= 10)
            {
                txtCountDown.Foreground = Brushes.Yellow;
                //WindowStartupLocation = WindowStartupLocation.CenterScreen;
                this.Topmost = true; //窗体最前端显示
            }

        }
    }
}
